//
//  SeriesSelectingViewController.m
//  HuateVideo
//
//  Created by jun yuan on 12-9-18.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "SeriesSelectingViewController.h"
@interface SeriesView:UIView{
}

@end

@implementation SeriesView

-(id)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self) {
        self.backgroundColor=[UIColor clearColor];
    }
    return self;
}
-(void)drawRect:(CGRect)rect{
    UIImage* im=[UIImage imageNamed:@"seriesBg.png"];
    [im drawInRect:rect];
}
@end
@interface SeriesSelectingViewController ()

@end

@implementation SeriesSelectingViewController
@synthesize seriesMovie;
-(void)dealloc{
    [btnArray release];
    [selectedArray release];
    [seriesMovie release];
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        selectCount=0;
        btnArray=[[NSMutableArray alloc] init];
        selectedArray=[[NSMutableArray alloc] init];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = TOOLBAR_TINTCOLOR;
    self.title = LS(@"电视剧下载");
    UIButton* btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=CGRectMake(0,0,50,30);
    btn.titleLabel.font=[UIFont systemFontOfSize:14];
    [btn setTitle:LS(@"返回") forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"liangzianniu.png"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(dismissmodal) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem = barButtonItem;
    [barButtonItem release];
    [self initScrollView];
    [self topLabel];
}

- (void)topLabel{
    UILabel* infoLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 160, 50)];
    infoLabel.backgroundColor=[UIColor clearColor];
    infoLabel.textAlignment=UITextAlignmentCenter;
    infoLabel.font=[UIFont systemFontOfSize:16];
    infoLabel.text=[NSString stringWithFormat:@"%@%@",seriesMovie.News_title,LS(@"下载")];
    [self.view addSubview:infoLabel];
    [infoLabel release];
    partLabel=[[UILabel alloc] initWithFrame:CGRectMake(160, 0, 160, 50)];
    partLabel.font=[UIFont systemFontOfSize:14];
    partLabel.backgroundColor=[UIColor clearColor];
    partLabel.text=[NSString stringWithFormat:@"(%@%d%@,%@%d%@)",LS(@"共"),(seriesMovie.File_path_array.count/2),LS(@"集"),LS(@"已选"),selectCount,LS(@"集")];
    [self.view addSubview:partLabel];
    [partLabel release];
    
    UIButton* all_s=[UIButton buttonWithType:UIButtonTypeCustom];
    all_s.frame=CGRectMake(0, 460-44-44, 100, 44);
    all_s.backgroundColor=[UIColor grayColor];
    [all_s setTitle:LS(@"全选") forState:UIControlStateNormal];
    [all_s addTarget:self action:@selector(selectAll) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:all_s];
    
    UIButton* all_d=[UIButton buttonWithType:UIButtonTypeCustom];
    all_d.frame=CGRectMake(100, 460-44-44, 100, 44);
    all_d.backgroundColor=[UIColor grayColor];
    [all_d setTitle:LS(@"反选") forState:UIControlStateNormal];
    [all_d addTarget:self action:@selector(SelectNone) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:all_d];

    UIButton* downBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    downBtn.frame=CGRectMake(200, 460-44-44, 120, 44);
    downBtn.backgroundColor=[UIColor grayColor];
    [downBtn setTitle:LS(@"开始下载") forState:UIControlStateNormal];
    [downBtn addTarget:self action:@selector(beginDown) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:downBtn];

}

-(void)refreshCountLab{
    int c=0;
    for (UIButton* b in btnArray) {
        if (b.selected) {
            c++;
        }
    }
    partLabel.text=[NSString stringWithFormat:@"(%@%d%@,%@%d%@)",LS(@"共"),(seriesMovie.File_path_array.count/2),LS(@"集"),LS(@"已选"),c,LS(@"集")];

}
- (void)selectAll{
    for (UIButton*b in btnArray) {
        b.selected=YES;
    }
    [self refreshCountLab];
}

- (void)SelectNone{
    for (int i=0;i<btnArray.count;i++) {
        UIButton*b=[btnArray objectAtIndex:i];
        b.selected=!b.selected;
    }
    [self refreshCountLab];
}

-(void)btnSelectd:(UIButton*)sender{
    sender.selected=!sender.selected;
    [self refreshCountLab];
}

- (void)initScrollView{
    UIScrollView* scroll=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 55, 320, 300)];
    NSArray* temp=seriesMovie.File_path_array;
    scroll.contentSize=CGSizeMake(320, (temp.count/3)*60);

    for (int i=0; i<(temp.count/2); i++) {
        SeriesView*view=[[SeriesView alloc] initWithFrame:CGRectMake(100*(i%3), (i/3)*60, 100, 50)];
        [scroll addSubview:view];
        [view release];
        
        UIButton* bt=[UIButton buttonWithType:UIButtonTypeCustom];
        bt.frame=CGRectMake(10, 9, 32, 33);
        bt.tag=i;
        [bt setImage:[UIImage imageNamed:@"series_d.png"] forState:UIControlStateNormal];
        [bt setImage:[UIImage imageNamed:@"series_s.png"] forState:UIControlStateSelected];
        [bt addTarget:self action:@selector(btnSelectd:) forControlEvents:UIControlEventTouchUpInside];
        bt.selected=NO;
        [view addSubview:bt];
        [btnArray addObject:bt];
        
        UILabel*no_lab=[[UILabel alloc] initWithFrame:CGRectMake(60, 20, 30, 30)];
        no_lab.backgroundColor=[UIColor clearColor];
        no_lab.text=[temp objectAtIndex:i*2];
        [view addSubview:no_lab];
        [no_lab release];
    }
    [self.view addSubview:scroll];
    [scroll release];
}



- (void)beginDown{
    NSArray* temp=seriesMovie.File_path_array;
    for (int i=0; i<btnArray.count; i++) {
        UIButton*sender=[btnArray objectAtIndex:i];
        if (sender.selected) {
            [selectedArray addObject:[temp objectAtIndex:sender.tag*2]];
            [selectedArray addObject:[temp objectAtIndex:sender.tag*2+1]];
        }else {
            [selectedArray removeObject:[temp objectAtIndex:sender.tag*2]];
            [selectedArray removeObject:[temp objectAtIndex:sender.tag*2+1]];
        }
    }
    if (selectedArray.count) {
        seriesMovie.File_path_array=selectedArray;
        
        [[NSNotificationCenter defaultCenter] postNotificationName:SENDTODOWNLOAD_NOTIFICATION object:seriesMovie];
        [self dismissmodal];
    }else {
        UIAlertView* al=[[UIAlertView alloc] initWithTitle:LS(@"提示") message:LS(@"请选择要下载的剧集") delegate:nil cancelButtonTitle:LS(@"确定") otherButtonTitles:nil];
        [al show];
        [al release];
    }
}

- (void)dismissmodal{
    [self.navigationController dismissModalViewControllerAnimated:YES];
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
